#!/bin/bash
# 上传mobileprovision 小可怜+小白+小青 上

set -e

# shellcheck disable=SC2088
server_dst_path='~/tmp/tmp_mp'

# 上传 mobileprovision文件
function upload_file() {
  local server_address
  server_address="$1"
  if [[ $2 ]]; then
    # 上传 命令行参数里的单个文件
    mp_path="$2"
    echo -e "upload file: '${mp_path}' to '${server_address}:${server_dst_path}'\n"
    scp "${mp_path}" "${server_address}:${server_dst_path}"
  else
    # 上传 local_source_path目录里的所有mobileprovision文件
    local_source_path="${HOME}/Downloads"
    echo -e "find mobileprovision in -type f -name "file_name" dir: '${local_source_path}'\n"
    find "${local_source_path}" -maxdepth 1 -name "*.mobileprovision" -type f -print |
      while read tmp_mp_path; do
        echo -e "upload file: '${tmp_mp_path}' to '${server_address}:${server_dst_path}'\n"
        scp "${tmp_mp_path}" "${server_address}:${server_dst_path}"
      done
  fi
}

# 在远程服务器上执行sync_mp.py
function server_run_sync_mp() {
  local server_address
  server_address="$1"
  echo "⚡'$server_address': run sync_mp.py "
  ssh "${server_address}" <<eeooff
source ~/.zshrc
syncmobileprovision -D --path ~/tmp/tmp_mp
exit
eeooff
}

server_list=('okmobile@xkl.okappworld123.tk' 'xiaobai@xiaobai.okappworld123.tk' 'xiaoqing@xiaoqing.okappworld123.tk' 'okex@jenkins.okappworld123.tk')

for tmp_address in "${server_list[@]}"; do
  upload_file "$tmp_address" "$1"
  server_run_sync_mp "$tmp_address"
done

echo -e "\n*** 导入mobileprovision完成👍***\n"

set +e
